package com.qqtech.qquan.coupon.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.coupon.enums.CouponBaseBizzTypeDictEnum;
import com.qqtech.qquan.coupon.enums.CouponConst;
import com.qqtech.qquan.coupon.model.CouponBase;
import com.qqtech.qquan.coupon.model.CouponBaseQuery;
import com.qqtech.qquan.coupon.model.CouponBaseVo;
import com.qqtech.qquan.coupon.model.CouponSysParam;
import com.qqtech.qquan.coupon.service.CouponBaseService;
import com.qqtech.qquan.coupon.service.CouponSysParamService;
import com.qqtech.qquan.mall.model.MallShopVo;
import com.qqtech.qquan.mall.service.MallShopService;
import com.qqtech.qquan.user.model.UserBase;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/coupon/base")
public class CouponBaseController extends BaseWebController<CouponBase, CouponBaseQuery> {
	@Resource
	private CouponBaseService couponBaseService;

	@Resource
	private MallShopService mallShopService;
	@Resource
	private CouponSysParamService couponSysParamService;
	@Resource
	private UserBaseService userBaseService;

	@Override
	protected BaseService<CouponBase> getBaseService() {
		return couponBaseService;
	}

	/**
	 * 列表
	 * 
	 * @param idleBaseQuery
	 * @return
	 */
	@RequestMapping(value = "/page")
	@ResponseBody
	public Result page(@RequestBody CouponBaseQuery query) {
		Result result = new Result();
		query.setStatus(YesNoEnum.YES.getCode());
		query.setIsFullPrice(YesNoEnum.YES.getCode());
		query.setIsQBuy(YesNoEnum.YES.getCode());
		int communityId = query.getCommunityId();
		Integer loginUserId = super.getUserId();
		if (loginUserId != null) {
			UserBase userBase = userBaseService.getById(loginUserId);
			if (userBase != null && userBase.getCommunityId() != null && userBase.getCommunityId().intValue() != 0) {
				communityId = userBase.getCommunityId();
			}
		}
		query.setCommunityId(communityId);
		Pageable pageKit = new PageKit(query.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<CouponBaseVo> page = couponBaseService.queryPageList(query, pageKit);
		for (CouponBaseVo couponBaseVo : page) {
			couponBaseVo.setMinusPrice(couponBaseVo.getMinusPrice().multiply(new BigDecimal("100")));
			couponBaseVo.setBizzTypeDesc(CouponBaseBizzTypeDictEnum.getDesc(couponBaseVo.getBizzType()));
			if (couponBaseVo.getBizzType() == CouponBaseBizzTypeDictEnum.SHOP.getCode()) {
				MallShopVo mallShopVo = mallShopService.getById(couponBaseVo.getBizzId());
				couponBaseVo.setBizzTypeDesc(couponBaseVo.getBizzTypeDesc() + "-" + mallShopVo.getName());
			}
		}
		result.put("page", page);
		return result;
	}

	@RequestMapping(value = "/getCjRules")
	@ResponseBody
	public Result cjRules() {
		Result result = new Result();
		boolean hasData = false;
		CouponSysParam couponSysParam = couponSysParamService.getByKey(CouponConst.PARAM_COUPON_CJ_RULES);
		if (couponSysParam != null && StringUtil.isNotBlank(couponSysParam.getRemark())) {
			String[] ruleArr = couponSysParam.getRemark().split("#");
			if (ruleArr != null && ruleArr.length > 0) {
				List<CouponBase> ruleList1 = new ArrayList<CouponBase>(10);
				for (String string : ruleArr) {
					CouponBase base1 = new CouponBase();
					base1.setName(string);
					ruleList1.add(base1);
				}
				hasData = true;
				result.put("ruleList", ruleList1);
			}
		}
		if (hasData == false) {
			List<CouponBase> ruleList = new ArrayList<CouponBase>(10);
			CouponBase base = new CouponBase();
			base.setName("1.活动时间：2017年5月11日启动欢乐送（仅限桃源居用户参与）");
			ruleList.add(base);
			base = new CouponBase();
			base.setName("2.加入桃源居，在社区签到即可获得抽奖；转发或分享抽奖页面可再抽奖一次");
			ruleList.add(base);
			base = new CouponBase();
			base.setName("3.奖品包括：价值599元苏泊尔电饭煲、价值249元九阳豆浆机、价值89元海螺移动电源、价值38元苏家寨老酱、500圈币、200圈币");
			ruleList.add(base);
			base = new CouponBase();
			base.setName("4.实物奖励需填写用户地址，支付0.01元人民币即可免费配送到家（配送仅限桃源内）；虚拟奖品则直接发放到圈圈账户");
			ruleList.add(base);
			base = new CouponBase();
			base.setName("5.客服电话：13510415690/0755-82530651");
			ruleList.add(base);
			base = new CouponBase();
			base.setName("6.活动最终结束权归深圳幸福圈圈科技有限公司所有");
			ruleList.add(base);
			result.put("ruleList", ruleList);
		}
		return result;
	}
}
